package com.pronhub.luowowo.comment.service.impl;

public class cc {
/*

        public static final int STRATEGY_STATIS_TYPE_VIEWNUM = 1; //阅读数
        public static final int STRATEGY_STATIS_TYPE_REPLYNUM = 2; //回复数
        public static final int STRATEGY_STATIS_TYPE_FAVORNUM = 3; //收藏数
        public static final int STRATEGY_STATIS_TYPE_SHARENUM = 4; //分享数
        public static final int STRATEGY_STATIS_TYPE_THUMBSUPNUM = 5; //点赞数

        @Autowired
        private StringRedisTemplate template;
        @Reference
        private IStrategyDetailService strategyDetailService;

        @Override
        public StrategyStatisVO getStrategyStatisVo(Long sid) {
            String key = RedisKeys.STRATEGY_STATIS_VO.join(sid.toString());
            StrategyStatisVO vo = null;
            if(template.hasKey(key)){
                //如果存在
                String voStr = template.opsForValue().get(key);
                vo = JSON.parseObject(voStr, StrategyStatisVO.class);
            }else{
                //如果不存在
                vo = new StrategyStatisVO();
                //初始化属性
                StrategyDetail detail = strategyDetailService.get(sid);
                //属性拷贝
                BeanUtil.copyProperties(vo, detail);
                vo.setDestId(detail.getDest().getId());
                vo.setDestName(detail.getDest().getName());
                vo.setStrategyId(detail.getId());
                template.opsForValue().set(key, JSON.toJSONString(vo));
            }
            return vo;
        }


        private void numIncrease(Long sid, int num,int type){
            //1: 判断strategy_statis_vo:sid 的key是否存在
            String key = RedisKeys.STRATEGY_STATIS_VO.join(sid.toString());
            StrategyStatisVO vo = this.getStrategyStatisVo(sid);
            switch (type){
                case STRATEGY_STATIS_TYPE_VIEWNUM: //阅读
                    vo.setViewnum(vo.getViewnum() + num);
                    break;
                case STRATEGY_STATIS_TYPE_REPLYNUM: //回复
                    vo.setReplynum(vo.getReplynum() + num);
                    break;
                case STRATEGY_STATIS_TYPE_FAVORNUM: //收藏
                    vo.setFavornum(vo.getFavornum() + num);
                    break;
                case STRATEGY_STATIS_TYPE_SHARENUM: //分享
                    vo.setSharenum(vo.getSharenum() + num);
                    break;
                case STRATEGY_STATIS_TYPE_THUMBSUPNUM: //点赞
                    vo.setThumbsupnum(vo.getThumbsupnum() + num);
                    break;
            }
            //统一保存到redis
            template.opsForValue().set(key, JSON.toJSONString(vo));
        }
        @Override
        public void viewnumIncrease(Long sid, int num) {
            numIncrease(sid, num, STRATEGY_STATIS_TYPE_VIEWNUM);
        }

        @Override
        public void replynumIncrease(Long sid, int num) {
            numIncrease(sid, num, STRATEGY_STATIS_TYPE_REPLYNUM);
        }

        @Override
        public boolean favor(Long sid, Long uid) {
            boolean flag = false;

            //1:判断当前用户是否已经收藏了该篇攻略,
            String key = RedisKeys.STRATEGY_STATIS_FAVOR.join(sid.toString());
            List<Long> userIds = null;
            if(template.hasKey(key)){
                //有攻略的收藏用户列表
                String listStr = template.opsForValue().get(key);
                //参数2:元素的泛型
                userIds = JSON.parseArray(listStr, Long.class);
            }else{
                //没有攻略的收藏用户列表
                userIds = new ArrayList<>();
            }
            StrategyStatisVO vo = this.getStrategyStatisVo(sid);
            if(userIds.contains(uid)){
                //2:如果已经收藏, 表示当前操作是为取消收藏, 收藏数-1
                vo.setFavornum(vo.getFavornum() - 1);
                userIds.remove(uid);
            }else{
                //3:如果没收藏, 表示当前操作是为收藏, 收藏数+1:
                vo.setFavornum(vo.getFavornum() + 1);
                userIds.add(uid);
                flag = true;
            }
            //统一更新
            template.opsForValue().set(key, JSON.toJSONString(userIds));
            String voKey = RedisKeys.STRATEGY_STATIS_VO.join(sid.toString());
            template.opsForValue().set(voKey,  JSON.toJSONString(vo));

            return flag;
        }
*/


    /*public void viewnumIncrease(Long sid, int num) {
        //1: 判断strategy_statis_vo:sid 的key是否存在
        String key = RedisKeys.STRATEGY_STATIS_VO.join(sid.toString());
        StrategyStatisVO vo = null;
        if(template.hasKey(key)){
            //2: 如果存在, 通过key获取vo 对象 对 viewnum +1,
            String voStr = template.opsForValue().get(key);
            vo = JSON.parseObject(voStr, StrategyStatisVO.class);
            vo.setViewnum(vo.getViewnum() + 1);
        }else{
            //3:如果不存在, 创建vo对象, 初始化相关属性, 然后让viewnum + 1
            vo = new StrategyStatisVO();
            //初始化属性
            StrategyDetail detail = strategyDetailService.get(sid);

            //属性拷贝
            BeanUtil.copyProperties(vo, detail);
            vo.setDestId(detail.getDest().getId());
            vo.setDestName(detail.getDest().getName());
            vo.setStrategyId(detail.getId());

            vo.setViewnum(vo.getViewnum() + 1);
        }
        //统一保存到redis
        template.opsForValue().set(key, JSON.toJSONString(vo));

    }*/
}
